package pers.mihao.quickstream.common.plus.jdbc;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.ParameterizedTypeImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableId;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;
import pers.mihao.quickstream.common.exception.BaseException;
import pers.mihao.quickstream.common.model.BaseDO;
import pers.mihao.quickstream.common.plus.jdbc.blodfiled.BlobField;
import pers.mihao.quickstream.common.util.CollectionUtil;
import pers.mihao.quickstream.common.util.ReflectionUtil;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static pers.mihao.quickstream.common.util.ReflectionUtil.*;

/**
 * blod序列化
 */
@Component
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
})
public class TimeSetFiledInterceptor implements Interceptor {

    @Resource
    DataSource dataSource;

    private final String blobFiledName = "blob_data";
    private final String blobTableName = "blob_save";

    @Override
    public Object intercept(Invocation invoke) throws Throwable {
        if (invoke.getTarget() instanceof Executor) {
            Object[] args = invoke.getArgs();
            if (args.length == 2 && args[0] instanceof MappedStatement && args[1] instanceof BaseDO) {
                MappedStatement method = (MappedStatement) args[0];
                BaseDO baseDO = (BaseDO) args[1];
                if (method.getId().endsWith(".insert")) {
                    baseDO.setGmtCreate(new Date());
                    baseDO.setGmtModify(new Date());
                }
            }
        }
        return invoke.proceed();
    }

}
